
function ScrollLinker() {
	this.nodes = new Array();
}

ScrollLinker.prototype.add = function(node) {
	this.nodes[this.nodes.length] = node;
	node._scrollLinker = this;
  new EventListener(node, "scroll", function(evn) {
    var s = this._scrollLinker;
      var v = readPositioning(this);
    var ph = v.scrollTop / (v.scrollHeight - v.height);
    for (var i = 0; i < s.nodes.length; ++i) {
    	var e = s.nodes[i];
    	if (e == this)
    	  continue;
      var v = readPositioning(e);
    	var d = (v.scrollHeight - v.height) * ph;
    	if ((Math.abs(v.scrollTop - d) > 0.5)) {
    		if (e.scrollTo)
          e.scrollTo(0, Math.round(d));
        else
          e.scrollTop = Math.round(d);
    	}
    }
  });
}

new EventListener(window, "load", function(evn) {
  var pf1 = document.getElementById("preview1");
  var pf2 = document.getElementById("preview2");
  var linker = new ScrollLinker();
  new EventListener(pf1, "load", function(evn) {
    var cp = this.contentWindow;
    linker.add(cp);
  });
  new EventListener(pf2, "load", function(evn) {
    var cp = this.contentWindow;
    linker.add(cp);
  });
});

function readPositioning(e) {
	var p = new Object();
	if (e.document)
	  e = e.document.documentElement;
  p.scrollTop = e.scrollTop;
  p.scrollHeight = e.scrollHeight;
  p.height = e.offsetHeight;
	return p;
}
